Boook club R-Ladies Bergen, R-Ladies Den Bosch, R-Ladies Amsterdam
origin or upstream.https://github.com/OWNER/REPO.git orgit@github.com:OWNER/REPO.gitgit initusethis::use_git()Tools > Version Control > Project Setuporigin is configured with permission to push/pull.origin on GitHub is a source repo.origin is your primary repo.usethis::use_github().usethis::create_from_github("OWNER/REPO", fork = FALSE).git clone <URL>File > New Project > Version Control > Gitorigin is a source.origin is your primary repo.usethis::use_github(organisation = "ORGNAME").How does this setup happen?
Cloning the source repo (via git clone <URL> or Git client)
usethis::create_from_github("OWNER/REPO", fork = FALSE).
What if you do want to make a pull request?
origin) of the source repo (upstream).upstream.origin is your primary repo (push/pull permission).usethis::create_from_github("OWNER/REPO", fork = TRUE).usethis::create_from_github("OWNER/REPO", fork = TRUE).upstream remote.upstream/main as the upstream tracking branch for local main.git commitgit commit --amendThink of your project as a mountain you’re climbing.
Coding without commits is like free-climbing:
Using a commit is like using anchors when climbing.
If you make a mistake, you can’t fall past the previous commit
git commit --amendStart with your project in a functional state.
git status to check this.Initial status of project.
Make a small change towards a larger objective and commit it as WIP (work in progress)
git commit -m "WIP"
git commit --amend --no-editgit commit --amend -m "Implement awesome feature"
What if you make some changes that break your project?
A -- B -- C -- WIP*
WIP*.
WIP* state.How to achieve?
git reset --hardLet’s imagine you pushed this state to GitHub by mistake:
A -- B -- C -- WIP (85bf30a)
and proceeded to git commit --amend again locally, leading to this state:
A -- B -- C -- WIP* (6e884e6)
You have two choices:
git reset --hard HEAD^) and pull from GitHub.A -- B -- C -- WIP (85bf30a)
A -- B -- C -- WIP (85bf30a) -- E
You have two choices:
git push --force).A -- B -- C -- D
Our push is rejected!
$ git push
To https://github.com/YOU/REPO.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/YOU/REPO.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
This means that your local Git history and that on the GitHub remote are not compatible, i.e. they have diverged.
Use git status to figure out where the divergence has occurred.
Let’s assume this is what we see on GitHub:
A -- B -- C
This is what we see locally:
A -- B -- D
To reconcile the differences:
git pull)git push)A -- B -- C -- D
You want to pull upstream changes but have made local commits or uncommitted changes.
A--B--CA--B--DGoal: Incorporate C into your local branch without losing D or uncommitted changes.
A--B--CA--B--(uncommitted changes)Outcome: git pull works and fast-forward merge happens.
Remote: A--B--C
Local before 'git pull': A--B--(uncommitted changes)
Local after 'git pull': A--B--C--(uncommitted changes)
git stash works, sometimesgit pull won’t workNow what?
git stash is a way to temporarily store some changes to get them out of the way.git stash save
git pull
git stash pop
Outcome: git stash is successful.
git stash with conflictsgit stash popgit stash with conflictsgit stash pop did not go smoothly,
git resetgit stash dropOutcome: We did it!
A--B--C.A--B--D.git pull doesjenny@2015-mbp ethel $ git pull
Auto-merging foo.R
CONFLICT (content): Merge conflict in foo.R
Automatic merge failed; fix conflicts and then commit the result.
Mark the affected file foo.R as resolved via git add and make an explicit git commit to finalize this merge.
jenny@2015-mbp ethel $ git add foo.R
jenny@2015-mbp ethel $ git commit
[main 20b297b] Merge branch 'main' of github.com:jennybc/ethel
Outcome: We’ve achieved this:
Remote: A--B--C
Local before: A--B--D
Local after: A--B--D--(merge commit)
\_C_/
git pull --rebase creates a nicer history than git pull when integrating local and remote commits.
Outcome: If no conflicts, we get this:
Remote: A--B--C
Local before: A--B--D
Local after: A--B--C--D
git pullA--B--C.A--B--(uncommitted changes).[R-Ladies Book Club]